#include "PlayerData.h"

using namespace cocos2d;

const char* LEVEL_SCORE = "LEVEL_SCORE";
const char* LEVEL_COMPLETED = "LEVEL_COMPLETE";
const char* LEVEL_SELECTED = "LEVEL_SELECTED";
const char* LEVEL_PROGRESS = "LEVEL_PROGRESS";
const char* SKILL_SLOT_1 = "SKILL_SLOT_1";
const char* SKILL_SLOT_2 = "SKILL_SLOT_2";
const char* SKILL_SLOT_3 = "SKILL_SLOT_3";

const char* MUSIC_TOGGLE = "MUSIC_TOGGLE";
const char* SOUND_EFFECT_TOGGLE = "SOUND_EFFECT_TOGGLE";

//================ SETTERS ======================

/*!
    @function saveSkillNumber
    @param		slotNumber:int
				skill slot number to retrieve
				skillNumber: int
				skill number to save in the slot
    @result		save data into UserDefault
 */
void PlayerData::saveSkillNumber(int slotNumber, int skillNumber)
{
	switch(slotNumber)
	{
	case 1:
		CCUserDefault::sharedUserDefault()->setIntegerForKey(SKILL_SLOT_1, skillNumber);
		break;
	case 2:
		CCUserDefault::sharedUserDefault()->setIntegerForKey(SKILL_SLOT_2, skillNumber);
		break;
	case 3:
		CCUserDefault::sharedUserDefault()->setIntegerForKey(SKILL_SLOT_3, skillNumber);
		break;
	}
}

/*!
    @function saveSelectedLevel
    @param		levelNumber:int
				level number to save
    @result		save data into UserDefault
 */
void PlayerData::saveSelectedLevel(int levelNumber)
{
	CCUserDefault::sharedUserDefault()->setIntegerForKey(LEVEL_SELECTED, levelNumber);
}

/*!
    @function saveLevelProgress
    @param		levelNumber:int
				level number to save
    @result		save player's progress if more than what he has currently completed
 */
void PlayerData::saveLevelProgress(int levelNumber)
{
	if( levelNumber > PlayerData::getLevelProgress() )
	{
		CCUserDefault::sharedUserDefault()->setIntegerForKey(LEVEL_PROGRESS, levelNumber);
	}
}
/*!
    @function saveIsMusicMute
    @param		isMusic: bool
				is music mute
    @result		save data into UserDefault
 */
void PlayerData::saveIsMusicMute(bool isMusic)
{
	CCUserDefault::sharedUserDefault()->setBoolForKey(MUSIC_TOGGLE, isMusic);
}
/*!
    @function saveIsSoundEffectMute
    @param		isSoundEffect: bool
				is sound effect mute
    @result		save data into UserDefault
 */
void PlayerData::saveIsSoundEffectMute(bool isSoundEffect)
{
	CCUserDefault::sharedUserDefault()->setBoolForKey(SOUND_EFFECT_TOGGLE, isSoundEffect);
}

/*!
    @function saveLevelResults
    @param		levelNumber: int
				level number of level to retrieve
				score: int
				new score to save
				completed: bool
				if level is completed
    @result		save data into UserDefault
 */
void PlayerData::saveLevelResults(int levelNumber, int score, bool completed)
{
	if( levelNumber <= 0 )
			return;

	int levelLength = PlayerData::getNumOfDigits(levelNumber);

	if( score < 0 || score >= PlayerData::getLevelScore(levelNumber) )
	{
		int scoreKeyLength = strlen(LEVEL_SCORE) + levelLength;
		char levelScoreKey[scoreKeyLength]; // buffer for the char array
		sprintf(levelScoreKey,"%s%i", LEVEL_SCORE, levelNumber); // add into the buffer
		CCLog("Save Game (Score): %d | %d", CCUserDefault::sharedUserDefault()->getIntegerForKey(levelScoreKey), score);
		CCUserDefault::sharedUserDefault()->setIntegerForKey(levelScoreKey, score);
	}
	if( !completed && !PlayerData::getLevelCompleted(levelNumber) )
	{
		int completedKeyLength = strlen(LEVEL_COMPLETED) + levelLength;
		char levelCompletedKey[completedKeyLength]; // buffer for the char array
		sprintf(levelCompletedKey,"%s%i", LEVEL_COMPLETED, levelNumber); // add into the buffer
		CCUserDefault::sharedUserDefault()->setIntegerForKey(levelCompletedKey, completed);
	}
}



//================ GETTERS ======================
/*!
    @function getSkillNumber
    @param		slotNumber:int
				skill slot number to retrieve
    @result		returns the skill at the stored number
 */
int PlayerData::getSkillNumber(int slotNumber)
{
	int skillNumber = -1;
	switch(slotNumber)
	{
	case 1:
		skillNumber = CCUserDefault::sharedUserDefault()->getIntegerForKey(SKILL_SLOT_1);
		break;
	case 2:
		skillNumber = CCUserDefault::sharedUserDefault()->getIntegerForKey(SKILL_SLOT_2);
		break;
	case 3:
		skillNumber = CCUserDefault::sharedUserDefault()->getIntegerForKey(SKILL_SLOT_3);
		break;
	}
	return skillNumber;
}

/*!
    @function getLevelProgress
    @result		returns the player's level progression
 */
int PlayerData::getLevelProgress()
{
	return CCUserDefault::sharedUserDefault()->getIntegerForKey(LEVEL_PROGRESS);
}

/*!
    @function getSelectedLevel
    @result		returns the selected level number
 */
int PlayerData::getSelectedLevel()
{
	return CCUserDefault::sharedUserDefault()->getIntegerForKey(LEVEL_SELECTED);
}
/*!
    @function isMusicMute
    @result		returns true if music is mute
 */
bool PlayerData::isMusicMute()
{
	return CCUserDefault::sharedUserDefault()->getBoolForKey(MUSIC_TOGGLE);
}
/*!
    @function isSoundEffectMute
    @result		returns true if sound effect is mute
 */
bool PlayerData::isSoundEffectMute()
{
	return CCUserDefault::sharedUserDefault()->getBoolForKey(SOUND_EFFECT_TOGGLE);
}
/*!
    @function getLevelScore
    @param		levelNumber:int
				level number of level to retrieve
    @result		returns the stored level score if available
    			returns 0 by default
 */
int PlayerData::getLevelScore(int levelNumber)
{
	int keyLength = strlen(LEVEL_SCORE) + PlayerData::getNumOfDigits(levelNumber);
	char levelScoreKey[keyLength]; // buffer for the char array

	sprintf(levelScoreKey,"%s%i", LEVEL_SCORE, levelNumber); // add into the buffer
	return CCUserDefault::sharedUserDefault()->getIntegerForKey(levelScoreKey, 0);
}
/*!
    @function getLevelCompleted
    @param		levelNumber:int
				level number of level to retrieve
    @result		Checks if the level has been completed
    			returns false by default
 */
bool PlayerData::getLevelCompleted(int levelNumber)
{
	int keyLength = strlen(LEVEL_COMPLETED) + PlayerData::getNumOfDigits(levelNumber);
	char levelCompletedKey[keyLength]; // buffer for the char array

	sprintf(levelCompletedKey,"%s%i", LEVEL_COMPLETED, levelNumber); // add into the buffer
	return CCUserDefault::sharedUserDefault()->getIntegerForKey(levelCompletedKey, 0);
}
/*!
    @function getNumOfDigits
    @param		levelNumber:int
				level number of level to retrieve
    @result		Checks the number of digits in the level
    			Used to concat string. Default to 0.
 */
int PlayerData::getNumOfDigits(int levelNumber)
{
	if(levelNumber < 10)
		return 1;
	if(levelNumber < 100)
		return 2;
	if(levelNumber < 1000)
		return 3;
	return 0;
}
